# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=swig
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.2.1
pkgrel=1
pkgdesc="Generate scripting interfaces to C/C++ code (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://www.swig.org/"
msys2_repository_url="https://github.com/swig/swig"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-pcre2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-boost"
              "${MINGW_PACKAGE_PREFIX}-python"
              "${MINGW_PACKAGE_PREFIX}-lua"
              "${MINGW_PACKAGE_PREFIX}-ruby")
source=(https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz
        pathtools.c
        pathtools.h
        001-relocate.patch
        002-fix-python-find.patch)
sha256sums=('fa045354e2d048b2cddc69579e4256245d4676894858fcf0bab2290ecf59b7d8'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            'a27041d7b13a17547f8a534d80c370a9e689f8f2784d150fc755cbb91382dac4'
            '057f0a306aee0d28a5bb6538b569c650e47c05b9848c9477cdeb7bb8c792944d')

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] Source/Swig/

  patch -p1 -i "${srcdir}"/001-relocate.patch
  patch -p1 -i "${srcdir}"/002-fix-python-find.patch

  ./autogen.sh
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure\
    --prefix=${MINGW_PREFIX} \
    --without-perl5 \
    --without-guile \
    --without-python \
    --with-python3=${MINGW_PREFIX}/bin/python

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make -k check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE-UNIVERSITIES "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE-UNIVERSITIES
}
